package kr.saeildamil.androidforstudy2.ch33;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class ChangeVolume extends Activity {
	MediaPlayer mPlayer;
	AudioManager mAudio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_changevolume);

		mPlayer = new MediaPlayer();
		String SDPath = Environment.getExternalStorageDirectory()
				.getAbsolutePath();

		try {
			mPlayer.setDataSource(SDPath + "/themeoftrixie.mp3");
			mPlayer.prepare();
			mPlayer.start();
		} catch (Exception e) {
			e.printStackTrace();
		}

		mAudio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

		SeekBar seekbar = (SeekBar) findViewById(R.id.seekvolume);
		seekbar.setMax(mAudio.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		seekbar.setProgress(mAudio.getStreamVolume(AudioManager.STREAM_MUSIC));
		seekbar.setOnSeekBarChangeListener(mOnSeek);
	}

	OnSeekBarChangeListener mOnSeek = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
					AudioManager.FLAG_SHOW_UI);
		}
	};

	@Override
	protected void onPause() {
		super.onPause();
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btnplay:
			if (mPlayer.isPlaying()) {
				mPlayer.pause();
			} else {
				mPlayer.start();
			}
			break;
		case R.id.btnvoldown:
			mAudio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI
							| AudioManager.FLAG_ALLOW_RINGER_MODES
							| AudioManager.FLAG_PLAY_SOUND
							| AudioManager.FLAG_VIBRATE);
			break;
		case R.id.btnvolup:
			mAudio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI
							| AudioManager.FLAG_ALLOW_RINGER_MODES
							| AudioManager.FLAG_PLAY_SOUND
							| AudioManager.FLAG_VIBRATE);
			break;
		case R.id.btnring:
			switch (mAudio.getRingerMode()) {
			case AudioManager.RINGER_MODE_NORMAL:
				Toast.makeText(this, "Now : Bell", Toast.LENGTH_SHORT).show();
				mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				Toast.makeText(this, "Now : Silent", Toast.LENGTH_SHORT).show();
				break;
			case AudioManager.RINGER_MODE_SILENT:
				Toast.makeText(this, "Now : Silent", Toast.LENGTH_SHORT).show();
				mAudio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				Toast.makeText(this, "Now : Vibrate", Toast.LENGTH_SHORT).show();
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				Toast.makeText(this, "Now : Vibrate", Toast.LENGTH_SHORT).show();
				mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				Toast.makeText(this, "Now : Bell", Toast.LENGTH_SHORT).show();
				break;
			}
			break;
		case R.id.btneffect1:
			// mAudio.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
			mAudio.playSoundEffect(AudioManager.FX_KEY_CLICK);
			break;
		case R.id.btneffect2:
			mAudio.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
			break;
		case R.id.btneffect3:
			mAudio.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
			break;
		}
	}

}
